import "../../css/adminlte.min.css";
import '../../css/app.css';
import req from '../../utils/request';
import qs from 'qs';
//获取 DOM 元素
let usernameInput = document.querySelector('input[name=username]');
let img = document.querySelector('img');
let form = document.querySelector('form');

//获取 search 查询字符串
let {search} = location;
search = search.slice(1);
//获取 id 参数
let {id} = qs.parse(search);

//获取当前用户的信息
async function getUser(){
  //发送请求获取用户数据
  let res = await req.get('/admin/user/'+id);
  //修改用户名 value 的值
  usernameInput.value = res.username;
  img.src = 'http://127.0.0.1:8081' + res.profile;
}

getUser();

form.onsubmit = async function(e){
  //阻止默认行为
  e.preventDefault();
  //收集表单数据
  let formData = new FormData(form);
  //发送请求  PUT  /admin/user/:id
  await req.put('/admin/user/' + id, formData);
  alert('更新成功');
}